package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverOilBiz;
import com.egao.orderB.entity.DriverOil;
import com.egao.orderB.service.DriverOilService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.egao.orderB.mapper.DriverOilMapper;
//import javax.transaction.Transactional;

/**
 * 油卡服务实现类
 * @author : Doman
 * @date : 2021-05-28 16:41:04
 * @version ：1.0
 */
@Service
public class DriverOilServiceImpl /*extends ServiceImpl<DriverOilMapper, DriverOil> */implements DriverOilService {

	@Autowired
	private DriverOilBiz driverOilBiz;

    /**
    @Override
    public PageResult<DriverOil> listPage(PageParam<DriverOil> page) {
        List<DriverOil> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<DriverOil> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, DriverOil driverOil, User loginUser)  throws Exception{
        return driverOilBiz.list(page,driverOil,loginUser);
    }

    @Override
    public JsonResult list(DriverOil driverOil, User loginUser) {
        return JsonResult.ok().setData(driverOilBiz.list(driverOil,loginUser));
    }

    @Override
    public JsonResult getById(DriverOil driverOil) {
        return JsonResult.ok().setData(driverOilBiz.getById(driverOil));
    }

    @Override
    public JsonResult getLastByCard(DriverOil driverOil) {
        return JsonResult.ok().setData(driverOilBiz.getLastByCard(driverOil));
    }

    @Transactional
    @Override
    public JsonResult save(DriverOil driverOil, User loginUser) {
        float total = 0;
        float balance = 0;
        try {
            DriverOil last = driverOilBiz.getLastByCard(driverOil);
            total = last.getCumulativePrice();
            balance = last.getBalancePrice();
        } catch (Exception e) {
            e.printStackTrace();
        }
        driverOil.setCumulativePrice(driverOil.getRechargePrice() + total);
        driverOil.setBalancePrice(driverOil.getRechargePrice()-driverOil.getUsePrice() + balance);
        if (driverOilBiz.save(driverOil, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(DriverOil driverOil, User loginUser) {
        if (driverOilBiz.update(driverOil, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(DriverOil driverOil, User loginUser) {
        if (driverOilBiz.remove(driverOil, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
